import {download,fileutil} from '../index.js'
import test from 'ava'
import fs from 'fs'

test('download json', async t => {
  let url = `https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php`;
  let ins = new download(url);
  let res = await ins.json();
  t.is(res.msg, '参数错误');
});

test('download html', async t => {
  let url = `https://www.baidu.com`;
  let ins = new download(url);
  let res = await ins.html();
  t.is(res.indexOf('www.baidu.com') > -1, true);
});


test('download video', async t => {
  let url = `https://minioapi.byyui.com:9999/zztemp/test.mp4`
  let ins = new download(url, './3.mp4');
  ins.on('progress', info => {
    if (info.speed) {
      // console.log(info.speed,info.sendStr);  
    }
  })
  let rs = await ins.download();
  t.is(rs.total, '2430794');
  fileutil.deldir('./3.mp4');
});

test('download m3u8 to mp4', async t => {
  t.timeout(100 * 1000);
  let url = `http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8`;
  let ins = new download(url, './4.mp4');
  await ins.downloadM3u8(20);
  let rs = fileutil.existsFile('./4.mp4');
  t.is(rs, true);
  fileutil.deldir('./4.mp4');
})